package siga.visao_busca;

import java.sql.SQLException;
import siga.banco.conexao;
import siga.visao.index;
import siga.visao.verErros;

/**
 *
 * @author rafaelfrasson
 */
public class buscaGradeCurricular extends javax.swing.JDialog {

    /** Creates new form buscaGradeCurricular */
    public buscaGradeCurricular(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jtfCodCurso = new javax.swing.JTextField();
        jtfDesGrad = new javax.swing.JTextField();
        jbBuscar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtGradeC = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Busca por Grade Curricular");

        jLabel1.setText("Curso:");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText("Nome da Grade Curricular:");
        jLabel2.setName("jLabel2"); // NOI18N

        jtfCodCurso.setName("jtfCodCurso"); // NOI18N
        jtfCodCurso.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfCodCursoKeyPressed(evt);
            }
        });

        jtfDesGrad.setName("jtfDesGrad"); // NOI18N
        jtfDesGrad.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfDesGradKeyPressed(evt);
            }
        });

        jbBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/busca_16x16.png"))); // NOI18N
        jbBuscar.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jbBuscar.setName("jbBuscar"); // NOI18N
        jbBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbBuscarActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jtGradeC.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Curso", "Grade Curricular"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jtGradeC.setName("jtGradeC"); // NOI18N
        jtGradeC.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtGradeCMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtGradeC);
        jtGradeC.getColumnModel().getColumn(0).setPreferredWidth(100);
        jtGradeC.getColumnModel().getColumn(0).setMaxWidth(100);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtfCodCurso, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jtfDesGrad, javax.swing.GroupLayout.DEFAULT_SIZE, 403, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbBuscar)))
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 579, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jtfCodCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtfDesGrad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jbBuscar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbBuscarActionPerformed
        busca();
    }//GEN-LAST:event_jbBuscarActionPerformed

    private void jtGradeCMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtGradeCMouseClicked
        if(evt.getClickCount()==2){
            javax.swing.table.DefaultTableModel jtg = (javax.swing.table.DefaultTableModel) jtGradeC.getModel();
            index.LRcurso.add(0, jtg.getValueAt(jtGradeC.getSelectedRow(), 0));
            dispose();
        }
    }//GEN-LAST:event_jtGradeCMouseClicked

    private void jtfCodCursoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfCodCursoKeyPressed
        if(evt.getKeyCode()==10){
            busca();
        }
    }//GEN-LAST:event_jtfCodCursoKeyPressed

    private void jtfDesGradKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfDesGradKeyPressed
        if(evt.getKeyCode()==10){
            busca();
        }
    }//GEN-LAST:event_jtfDesGradKeyPressed

    private void busca(){
        try {
            String curso = (jtfCodCurso.getText().equals("")) ? "" : " and cod_curso=" + jtfCodCurso.getText();
            String descricao = (jtfDesGrad.getText().equals("")) ? "" : " and descricao=" + jtfDesGrad.getText();
            conexao.Query("select cod_curso, descricao from grade_curricular where cod_emp=" + index.cod_emp + " " + curso + descricao + " group by cod_curso order by descricao ");
            javax.swing.table.DefaultTableModel jtg = (javax.swing.table.DefaultTableModel) jtGradeC.getModel();
            for (int x = jtGradeC.getRowCount() - 1; x >= 0; --x) {
                jtg.removeRow(x);
            }
            while (conexao.rs.next()) {
                jtg.addRow(new Object[]{conexao.rs.getString("cod_curso"),conexao.rs.getString("descricao")});
            }
        } catch (SQLException ex) {
            verErros.setErro(ex.getMessage());
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbBuscar;
    private javax.swing.JTable jtGradeC;
    private javax.swing.JTextField jtfCodCurso;
    private javax.swing.JTextField jtfDesGrad;
    // End of variables declaration//GEN-END:variables

}
